package com.yssoft.service.core;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.core.TMaterial;
import com.yssoft.mapper.core.TMaterialMapper;
import com.yssoft.shiro.MyRealm;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UploadFile;
import com.yssoft.utils.UserHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TMaterialService {

	@Resource
	private TMaterialMapper tMaterialMapper;

	public List<Map> queryNoMaterialByALLId(Integer id) {
		Map<String,Object> params=new HashMap<>();
		params.put("delete_flag", "0");
		params.put("tEId", UserHelper.getCurrentCompanyId());
		params.put("id", id);
		return tMaterialMapper.queryNoMaterialById(params);
	}

	public List<Map> queryYesMaterialByALLId(Integer id) {
		Map<String,Object> params=new HashMap<>();
		params.put("delete_flag", "0");
		params.put("tEId", UserHelper.getCurrentCompanyId());
		params.put("id", id);
		return tMaterialMapper.queryYesMaterialById(id);
	}

	public List<Map> queryMaterialAll() {
		Map<String,Object> params=new HashMap<>();
		params.put("delete_flag", "0");
		params.put("tEId", UserHelper.getCurrentCompanyId());
		return tMaterialMapper.queryMaterialAll(params);
	}
    
    
    /**
     * 分页查询物料管理
     * @param page
     * @param limit
     * @param name
     * @param manufacturer
     * @return
     */
    public JSONObject queryMaterialPage(int page, int limit, String name, String manufacturer) {
        Integer entId =UserHelper.getCurrentCompanyId();//企业id
        PageHelper.startPage(page,limit);//下面第一个查询结果进行分页
        Map<String,Object> param=new HashMap<>();
        param.put("companyId",entId);
        param.put("materialsName",name);
        param.put("manufacturer",manufacturer);
        List<Map<String,Object>> materialList  =tMaterialMapper.selectMaterialAll(param);
        int count = tMaterialMapper.selectMaterialCount(param);//查询条数
        return LayuiUtil.getJSONObjectByMap(materialList,count,"加载中","");
    }
    
    /**
     * 查询物料信息
     * @param materialId
     * @return
     */
    public TMaterial queryMaterialById(Integer materialId) {
        return tMaterialMapper.selectByPrimaryKey(materialId);
        
    }
    
    /**
     * 单条删除物料信息
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteMaterialById(String id) {
        TMaterial material=new TMaterial();
        material.setId(Integer.parseInt(id));
        material.setDeleteFlag("1");
        return tMaterialMapper.updateByPrimaryKeySelective(material);
    }
    /**
     * 多条删除物料信息
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteMaterialByIds(String ids) {
        String[] idsArray = ids.split(",");
        return tMaterialMapper.updateByIds(idsArray);
    }
    
    /**
     * 添加物料信息
     * @param material
     */
    @Transactional(rollbackFor = Exception.class)
    public void addMaterial(TMaterial material,MultipartFile file) {
        MyRealm.ShiroUser user = UserHelper.getCurrentUser();
        Map<String, Object> urlMap = UploadFile.compressUploadedFile(file);
        material.setmImg(urlMap.get("fileName")+"");
        material.setmTimg(urlMap.get("thumbnails")+"");
        material.setDeleteFlag("0");
        material.setCreateTime(new Date());
        material.setCreatorId(user.getPersonId());
        material.setCreatorName(user.getPersonName());
        material.settEId(user.getEntId());
        tMaterialMapper.insertSelective(material);
    }
    
    /**
     * 修改物料信息
     * @param material
     */
    @Transactional(rollbackFor = Exception.class)
    public void editMaterial(TMaterial material, MultipartFile file) {
        if(file.getOriginalFilename().length()>0){
            Map<String, Object> urlMap = UploadFile.compressUploadedFile(file);
            material.setmImg(urlMap.get("fileName")+"");
            material.setmTimg(urlMap.get("thumbnails")+"");
        }
        tMaterialMapper.updateByPrimaryKeySelective(material);
    }
}
